import java.io.EOFException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.jibble.pircbot.Colors;

public class RespawnThread extends Thread {
	String query;
	float strkoef1, strkoef2, intkoef1, intkoef2, agikoef1, agikoef2, hpkoef,
			manakoef, strstaminakoef, agistaminakoef, intstaminakoef;
	int interval;
	Connection con;
	DatabaseConnect dc;
	Shalatik bot;
	boolean running = true;
	String channel;

	String prevnick = null;
	String prevword = null;
	Long prevtime = null;

	public RespawnThread(Connection con, Shalatik bot, String channel,
			float strkoef1, float strkoef2, float intkoef1, float intkoef2,
			float agikoef1, float agikoef2, float hpkoef, float manakoef,
			int interval, float strstaminakoef, float agistaminakoef,
			float intstaminakoef) {
		this.con = con;
		this.bot = bot;
		this.channel = channel;
		this.strkoef1 = strkoef1;
		this.strkoef2 = strkoef2;
		this.intkoef1 = intkoef1;
		this.intkoef2 = intkoef2;
		this.agikoef1 = agikoef1;
		this.agikoef2 = agikoef2;
		this.hpkoef = hpkoef;
		this.manakoef = manakoef;
		this.interval = interval;
		this.strstaminakoef = strstaminakoef;
		this.agistaminakoef = agistaminakoef;
		this.intstaminakoef = intstaminakoef;
	}

	boolean checkSpam(ResultSet rs) {
		return true;
	}

	@Override
	public synchronized void run() {
		try {
			dc = new DatabaseConnect(con);
			while (running) {
				wait(interval);
				System.out.println("[debug] checking respawn");
				query = "SELECT * FROM irc_qog";
				ResultSet rs = dc.sendQuery(query);
				while (rs.next()) {
					int level = rs.getInt("level");
					String user = rs.getString("nick");
					if (rs.getInt("HP") <= 0) {
						// nastavovanie statsov ak su nulove, len ak je aj klasa
						if (rs.getString("CLASS").equals("agi")) {
							query = "UPDATE irc_qog SET HP = "
									+ (agikoef1 * hpkoef * level) + ", mana = "
									+ (agikoef2 * manakoef * level) + ", stamina = "
									+ (15 * level) + ", xp = 0"
									+ " WHERE nick = '" + user + "'";
							dc.sendUpdateQuery(query);
							bot.sendMessage(channel, Colors.GREEN
									+ rs.getString("nick") + Colors.NORMAL
									+ " has respawned!");
						}
						if (rs.getString("CLASS").equals("int")) {
							query = "UPDATE irc_qog SET HP = "
									+ (intkoef1 * hpkoef * level) + ", mana = "
									+ (intkoef2 * manakoef * level) + ", stamina = "
									+ (10 * level) + ", xp = 0"
									+ " WHERE nick = '" + user + "'";
							dc.sendUpdateQuery(query);
							bot.sendMessage(channel, Colors.GREEN
									+ rs.getString("nick") + Colors.NORMAL
									+ " has respawned!");
						}
						if (rs.getString("CLASS").equals("str")) {
							query = "UPDATE irc_qog SET HP = "
									+ (strkoef1 * hpkoef * level) + ", mana = "
									+ (strkoef2 * manakoef * level) + ", stamina = "
									+ (10 * level) + ", xp = 0"
									+ " WHERE nick = '" + user + "'";
							System.out.println(query);
							dc.sendUpdateQuery(query);
							bot.sendMessage(channel, Colors.GREEN
									+ rs.getString("nick") + Colors.NORMAL
									+ " has respawned!");
						}
					}
				}
				rs.close();
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (EOFException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public void turnOff() {
		running = false;
	}
}
